const utils = require('util');
const request = utils.promisify(require('request'));
const url = 'https://www.tianqiapi.com/api/';
const getWeather = (city = '朝阳') => {
  const sendJson = {
    'version': 'v1',
    'city': city
  };

  return request({
    url: url,
    method: 'POST',
    json: true,
    body: sendJson
  }).then(({body}) => {
    const data = body.data[0];
    // console.log(data);
    const win = data.win.length > 1 ? data.win.join('和') : data.win;
    const xj = data.index.map((item) => item.desc);
    const msg = `今天是${data.date}，${data.week}, ${data.wea}, ${win}。\n ${xj.join('\n')} \n ${data.air_tips}`;
    // console.log(msg);
    return msg;
  });
};

module.exports = getWeather;
